<template>
	<view class="main">
		<view class="search-box">
			<view>
				<u-search placeholder="请输入事项名称" search-icon-color="#999999" bg-color="#eeeeee" height="63"
					v-model="searchValue" @search="searchList" :show-action="false"></u-search>
			</view>
		</view>
		<view class="content-all">
			<scroll-view style="height: 100%;" scroll-y @scrolltolower="lower"  @scroll="scroll" :scroll-top="scrollTop">
				<view class="content" v-for="(item,index) in dataList" :key="index">
					<view>
						<text class="title">{{item.name}}</text>
						<text class="state">{{item.status}}</text>
					</view>
					<view>
						<text class="date">{{item.time}}</text>
						<view class="btn-group">
							<button class="btn-look" @click="toBanJianView(item.code,item.busId,item.name)">查看</button>
							<button v-if="item.correct == 1" class="btn-updata"
								@click="toBanJianUpdata(item.code,item.name,index)">更正</button>
							<button v-if="item.evaluate == 1" class="btn-evaluate"
								@click="toEvaluate(item.code,item.name,item.organName,item.time,index)">评价</button>
							<button v-if="item.evaluate == 2" class="btn-evaluated">已评价</button>
						</view>
					</view>
				</view>
				<uni-load-more :status="status" v-if="dataList.length != 0"></uni-load-more>
				<view class="noData" v-if="status == 'noMore' && dataList.length == 0">
					<image src="../../static/img_nodata.png"></image>
				</view>
			</scroll-view>
		</view>
		<u-back-top :scroll-top="scrollTop" @click.native="backTop"></u-back-top>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchValue: '',
				page: 1,
				allPages: 0,
				total: 0,
				dataList: [],
				status: 'more',
				pageNum: 1,
				pageSize: 20,
				listIndex: 0,
				scrollTop:0,
			};
		},
		watch: {
			searchValue(newVal, oldVal) {
				if (/^\s*$/.test(newVal)) {
					this.pageNum = 1
					this.dataList = [];
					this.getBanJianList()
				}
			}
		},
		onLoad() {
			uni.$off('update')
			uni.$on('update', (data) => {
				if (data.type == '材料更正') {
					this.dataList[this.listIndex].correct = '0';
				}
				if (data.type == '提交评价') {
					this.dataList[this.listIndex].evaluate = '2';
				}
			})
			this.getBanJianList();
		},
		onPullDownRefresh(){
			this.pageNum = 1;
			this.dataList = [];
			this.getBanJianList();
		},
		methods: {
			backTop(){
				this.scrollTop = 0;
			},
			scroll(e){
				this.scrollTop = e.detail.scrollTop;
			},
			lower() {
				if (this.status === 'noMore') {
					return;
				}
				this.pageNum++;
				this.getBanJianList();
			},
			toBanJianView(code, busId, name) {
				uni.navigateTo({
					url: '/pages/mineBanJian/banJianView?code=' + code + '&busId=' + busId + '&name=' + name
				});
			},
			toBanJianUpdata(code, name, index) {
				this.listIndex = index;
				uni.navigateTo({
					url: '/pages/mineBanJian/banJianUpdata?code=' + code + '&name=' + name
				});
			},
			toEvaluate(code, name, organName, time, index) {
				this.listIndex = index;
				uni.navigateTo({
					url: '/pages/matter/evaluate?code=' + code + '&name=' + name + '&organName=' + organName +
						'&time=' + time
				});
			},
			// 搜索
			searchList() {
				// 将页数置为1，数据列表重置为空
				this.pageNum = 1
				this.dataList = [];
				this.getBanJianList()
			},
			// 获取数据列表
			getBanJianList() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				this.status = 'loading';
				this.$ajax({
					url: '/api/wwwNews/selectMyItem',
					method: 'get',
					needAuth: true,
					data: {
						accessToken: uni.getStorageSync('access_token'),
						userType: uni.getStorageSync('userType'),
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						itemName: this.searchValue
					}
				}).then(res => {
					if(res.code == 200){
						var data = res.data.records;
						if (data.length === 0 && this.dataList.length === 0) {
							this.status = 'noMore';
						} else if (this.pageNum < Math.ceil(res.data.total / this.pageSize)) {
							this.status = 'more';
						} else {
							this.status = 'noMore';
						}
						this.dataList = this.dataList.concat(data);
					}
				}).finally(() => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				});
			}
		}
	};
</script>

<style lang="less" scoped>
	.main {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		height: 100%;
		overflow: hidden;
		display: flex;
		flex-direction: column;

		.noData {
			display: flex;
			justify-content: center;
			align-items: center;
			padding-top: 30rpx;

			image {
				width: 408rpx;
				height: 319rpx;
			}
		}

		.search-box {
			padding: 20rpx 0rpx;
			border-bottom: 1rpx solid #eeeeee;

			view {
				padding: 0rpx 30rpx;
			}
		}

		.content-all {
			padding: 0rpx 0rpx;
			flex: 1;
			overflow: hidden;

			.content {
				padding: 40rpx 30rpx 30rpx 30rpx;
				box-shadow: 2rpx 12rpx 27rpx 0rpx rgba(135, 136, 141, 0.15);
				border-radius: 12rpx;
				margin: 0 30rpx;
				margin-top: 30rpx;

				view {
					display: flex;
					align-items: center;

					&:nth-child(1) {
						margin-bottom: 44rpx;
					}

					.title {
						width: 340rpx;
						font-size: 28rpx;
						color: #333333;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}

					.state {
						flex: 1;
						text-align: right;
						font-size: 22rpx;
						color: #1975ff;
						opacity: 0.8;
					}

					.date {
						font-size: 24rpx;
						color: #999999;
						flex: 1;
					}

					.btn-group {
						button {
							width: 128rpx;
							height: 48rpx;
							line-height: 48rpx;
							font-size: 24rpx;
							color: #ffffff;
							border-radius: 6rpx;
							margin-left: 20rpx;
						}

						// 去除uni-app默认边框
						button:after {
							border: none;
						}

						.btn-look {
							background-color: #5b96ff;
						}

						.btn-updata {
							background-color: #ff6a6a;
						}

						.btn-evaluate {
							background-color: #62c5f8;
						}

						.btn-evaluated {
							background-color: #f6f6f6;
							color: #b4b4b4;
						}
					}
				}
			}
		}
	}
</style>
